package cn.mandifal.user.controller;
import java.util.List;

import cn.mandifal.pojo.DpUser;
import cn.mandifal.user.service.UserService;
import cn.mandifal.utils.PhoneFormatCheckUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;

import entity.PageResult;
import entity.Result;
/**
 * controller
 * @author shun
 *
 */
@RestController
@RequestMapping("/userController")
public class UserController {

	@Reference
	private UserService userService;
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<DpUser> findAll(){
		return userService.findAll();
	}
	
	
	/**
	 * 返回全部列表
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult  findPage(int page,int rows){			
		return userService.findPage(page, rows);
	}
	
	/**
	 * 增加
	 * @param user
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody DpUser user,String smsCode){
		//校验验证码是否正确
		boolean checkSmsCode = userService.checkSmsCode(user.getPhone(), smsCode);
		if(!checkSmsCode){
			return Result.failed("验证码不正确");
		}
		//判断是否存在此用户名
		if (null!= userService.findOneUser(user.getUsername())){
			return Result.failed("用户名已存在");
		}
		try {
			userService.add(user);
			return Result.success();
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed();
		}
	}
	/**
	 * 判断是否存在此用户名
	 */
	@RequestMapping("/checkUsername")
	public  Result checkUsername(String username){
		if (null == userService.findOneUser(username)){
			return Result.success();

		}else {
			return Result.failed();
		}
	}

	/**
	 * 发送验证码
	 * @param phone 手机号
	 * @return
	 */
	@RequestMapping("/sendCode")
	public Result sendCode(String phone){
		//判断手机号格式
		if(!PhoneFormatCheckUtils.isPhoneLegal(phone)){
			return Result.failed("手机格式不正确");
		}

		try {
			userService.createSmsCode(phone);
			return Result.success("验证码发送成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed("验证码发送失败");
		}
	}

	/**
	 * 修改
	 * @param user
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody DpUser user){
		try {
			userService.update(user);
			return Result.success("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed("修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @return
	 */
	@RequestMapping("/findOne")
	public DpUser findOne(){
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		if (! "anonymousUser".equals(username)){
			return userService.findOneUser(username);
		}
		return null;
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long [] ids){
		try {
			userService.delete(ids);
			return Result.success("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed("删除失败");
		}
	}
	
		/**
	 * 查询+分页
	 * @param pageNum 页码
	 * @param pageSize 页面大小
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody DpUser user, int pageNum, int pageSize  ){
		return userService.findPage(user, pageNum, pageSize);
	}


}
